package com.ldc.security.model;

import com.ldc.common.utils.CollectionUtils;
import com.ldc.model.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/6/11
 */
public class SecurityUser implements SecurityUserDetails {
    private SysUser userBean;
    private List<SecurityGrantedAuthority> authorityList;

    public SecurityUser(SysUser userBean, List<SecurityGrantedAuthority> authorityList) {
        this.userBean = userBean;
        this.authorityList = authorityList;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorityList;
    }

    @Override
    public String getPassword() {
        return getSysUser().getPassWord();
    }

    @Override
    public String getUsername() {
        return getSysUser().getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return getSysUser().isHasAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return getSysUser().isHasAccountNonLock();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return getSysUser().isHasCredentialNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return getSysUser().isEnabled();
    }

    @Override
    public SysUser getSysUser() {
        return userBean;
    }

    @Override
    public void setSysUser(SysUser userBean) {
        this.userBean = userBean;
    }

    @Override
    public void setAuthorities(Collection<SecurityGrantedAuthority> authorities) {
        if (CollectionUtils.isEmpty(authorities)) {
            this.authorityList = new ArrayList<>();
        }
        this.authorityList = new ArrayList<>(authorities);
    }
}
